﻿using System;
using Coolite.Ext.Web;
using CooliteExamples.Code.DAL;
using CooliteExamples.Code.DTO;
using CooliteExamples.Core.Exceptions;
using CooliteExamples.Core.Extensions;
using CooliteExamples.Web.Code;
using CooliteExamples.Web.Controllers;

namespace CooliteExamples.Web.Pages
{
		public partial class BookEntry : PageBase
		{
				protected void Page_Load(object sender, EventArgs e)
				{
						if (!Ext.IsAjaxRequest && !IsPostBack)
						{
								LoadPage();
						}
				}

				private void LoadPage()
				{
						// configure the Stores
						BookStore.Proxy.Add(new HttpProxy
																														{
																																DisableCaching = true,
																																Method = HttpMethod.GET,
																																Json = true,
																																Url = Core.ProcessController.Handlers.Book.GetAllBooksFromControllerHandlerUrl()
																														});

						CategoryStore.Proxy.Add(new HttpProxy()
																																		{
																																				DisableCaching = true,
																																				Method = HttpMethod.GET,
																																				Json = true,
																																				Url = Core.ProcessController.Handlers.Category.GetAllCategoriesHandlerUrl()
																																		});

						AuthorStore.Proxy.Add(new HttpProxy()
																																{
																																		DisableCaching = true,
																																		Method = HttpMethod.GET,
																																		Json = true,
																																		Url = Core.ProcessController.Handlers.Author.GetAuthorForBookFromControllerHandlerUrl()
																																});

						AddAuthorStore.Proxy.Add(new HttpProxy()
																																			{
																																					DisableCaching = true,
																																					Json = true,
																																					Method = HttpMethod.GET,
																																					Url = Core.ProcessController.Handlers.Author.GetAvailableAuthorsForBookHandlerUrl()
																																			});

						// configure the grids
						gpAuthors.AjaxEvents.Command.ExtraParams.Add(new Parameter()
																																																							{
																																																									Name = Core.Constants.AUTHOR_ID_PARAM_NAME,
																																																									Mode = ParameterMode.Raw,
																																																									Encode = true,
																																																									Value = "record.data.AuthorId"
																																																							});

						gpBooks.AjaxEvents.Command.ExtraParams.Add(new Parameter()
																																																					{
																																																							Name = Core.Constants.BOOK_ID_PARAM_NAME,
																																																							Mode = ParameterMode.Raw,
																																																							Encode = true,
																																																							Value = "record.data.BookId"
																																																					});
						gpBooks.AjaxEvents.Command.ExtraParams.Add(new Parameter()
																																																					{
																																																							Name = Core.Constants.COMMAND_NAME_PARAM_NAME,
																																																							Mode = ParameterMode.Raw,
																																																							Encode = false,
																																																							Value = "command"
																																																					});

						// setup the current controller
						CurrentController = new BookEntryController();
				}

				#region Event Handlers
				protected void gpBooks_Command(object sender, AjaxEventArgs ev)
				{
						int bookId = ev.ExtraParams[Core.Constants.BOOK_ID_PARAM_NAME].AsInt(0);
						string command = ev.ExtraParams[Core.Constants.COMMAND_NAME_PARAM_NAME];
						CurrentController.SelectedBookId = bookId;

						if (command == Core.Constants.EDIT_COMMAND)
						{
								PrepareViewForBook(CurrentController.CurrentBook);
								BookModifyWindow.SetTitle("Modify Book");
								BookModifyWindow.Show();
						}
						else
						{
								DeleteBookConfirmWindow.Show();
						}
				}

				protected void btnAdd_Click(object sender, AjaxEventArgs ev)
				{
						CurrentController.SelectedBookId = 0;

						PrepareViewForBook(CurrentController.CurrentBook);
						BookModifyWindow.SetTitle("Add Book");
						BookModifyWindow.Show();
				}

				protected void btnConfirmConfirm_Click(object sender, AjaxEventArgs ev)
				{
						try
						{
								BookDal.DeleteBook(CurrentController.CurrentBook);
								DeleteBookConfirmWindow.Hide();
								CurrentController.ResetBookList();
								BookStore.DataBind();
						}
						catch (BookManagerOperationException bex)
						{
								DisplayErrorMessage(bex.Message);
						}
				}

				protected void btnSave_Click(object sender, AjaxEventArgs e)
				{
						if (CurrentController.CurrentBook.Authors.Count == 0)
						{
								DisplayErrorMessage("You must select at least one author for this book");
								return;
						}

						CurrentController.CurrentBook.Title = txtTitle.Text;
						CurrentController.CurrentBook.ISBN = txtISBN.Text;
						CurrentController.CurrentBook.Category = new Category(cmbCategory.SelectedItem.Value.AsInt());

						BookDal.SaveBook(CurrentController.CurrentBook);
						CurrentController.ResetBookList();
						BookStore.DataBind();
						BookModifyWindow.Hide();
				}

				protected void btnAddAuthor_Click(object sender, AjaxEventArgs ev)
				{
						if (txtNewAuthorName.Text != string.Empty)
						{
								CurrentController.AddAuthorToCurrentBook(0, txtNewAuthorName.Text);
						}
						else
						{
								CurrentController.AddAuthorToCurrentBook(cmbAuthor.SelectedItem.Value.AsInt(), cmbAuthor.SelectedItem.Text);
						}

						AuthorStore.DataBind();
						AddAuthorWindow.Hide();
				}

				protected void gpAuthors_Command(object sender, AjaxEventArgs e)
				{
						try
						{
								int authorId = e.ExtraParams[Core.Constants.AUTHOR_ID_PARAM_NAME].AsInt();
								CurrentController.RemoveAuthorFromCurrentBook(authorId);

								AuthorStore.DataBind();
						}
						catch (BookManagerOperationException bex)
						{
								DisplayErrorMessage(bex.Message);
						}
				}
				#endregion

				#region Private Methods
				private void PrepareViewForBook(Book book)
				{
						txtTitle.Text = book.Title;
						txtISBN.Text = book.ISBN;

						cmbCategory.ClearValue();
						if (book.CategoryId > 0)
								cmbCategory.SetValue(book.CategoryId);

						AuthorStore.DataBind();
				}
				#endregion

				#region Properties
				public BookEntryController CurrentController
				{
						get { return (BookEntryController)Session[Core.Constants.CURRENT_CONTROLLER]; }
						set { Session[Core.Constants.CURRENT_CONTROLLER] = value; }
				}
				#endregion
		}
}
